package cn.mxz.service.impl;
import cn.mxz.mapper.UsersMapper;
import cn.mxz.pojo.ResultData;
import cn.mxz.pojo.UserLoginImage;
import cn.mxz.pojo.Users;
import cn.mxz.service.TbUserService;
import cn.mxz.utils.JwtUtils;
import cn.mxz.utils.MD5Utils;
import cn.mxz.utils.Rsautils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.security.PrivateKey;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者；朱洪旭
 * 时间；2021/12/2 15:34
 */
@Service
public class TbUserServiceImpl implements TbUserService {
    @Autowired
    private UsersMapper usersMapper;
    @Override
    public ResultData loginuser(Users users) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",users.getUsername());
        Users users1 = usersMapper.selectOne(queryWrapper);
        //账号不存在
        if(users1==null){
            return  new ResultData(100,"账户不存在");
        }
        if(users.getPassword().equals(users1.getPassword())){
            try {
                //登录成功
                users1.setPassword("");
                PrivateKey  privateKey1 = Rsautils.getPrivateKey1(new ClassPathResource("/rsa.pri").getInputStream());
                String token= JwtUtils.generateTokenExpireInSeconds(users1, privateKey1, 2000000);
                return new ResultData(0,"登录成功",new UserLoginImage(users1.getUserId(),token));
            } catch (Exception e) {
                //未知原因报错
                e.printStackTrace();
                return  new ResultData(102,"未知错误");
            }
        }else{
            //密码错误
                return  new ResultData(101,"密码错误");
        }
    }
    public  boolean checkemail(String email){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_email",email);
        Users users = usersMapper.selectOne(queryWrapper);
        if(users==null){
            return true;
        }
         return  false;
    }

    @Override
    public boolean checkeuser(String username, String phone) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        Users users = usersMapper.selectOne(queryWrapper);
       QueryWrapper queryWrapper1=new QueryWrapper();
       queryWrapper1.eq("user_mobile",phone);
        Users users1 = usersMapper.selectOne(queryWrapper1);
        if(users!=null){
            return  true;
        }
        if(users1!=null){
            return  true;
        }
            return false;
    }

    @Override
    public ResultData insertuser(Users users) {
        if(users.getUserImg()==null||users.getUserImg()==""){
            users.setUserImg("img/default.png");
        }
        Date date=new Date();
        users.setUserRegtime(date);
        users.setUserModtime(date);
        int insert = usersMapper.insert(users);
        if(insert>0){
            return  new ResultData(0,"添加成功");
        }
        return  new ResultData(118,"添加失败");
    }

    @Override
    public ResultData finduser(String username) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        Users users = usersMapper.selectOne(queryWrapper);
        if(users==null){return new ResultData(151,"该用户不存在请重新验证");}
        return  new ResultData(0,"查询成功",users);
    }

    @Override
    public ResultData usermsg(Integer id) {
        Users users = usersMapper.selectById(id);
        return  new ResultData(0,"查询成功",users);
    }
}
